<?php
declare(strict_types=1);

namespace house;


use app\admin\model\BusinessArea;
use app\admin\model\House;
use app\admin\model\HouseVillage;
use app\admin\model\Region;
use custom\Log;
use think\Exception;

/**
 * 编辑、新增 上架房源的拍卖纪录
 * 房源拍卖状态更新后，小区/商圈/省市区 的房源数变化
 * Class HouseCount2
 * @package house
 */
class HouseCount2 extends HouseCount {

    public static $region_field1 = 'village_id';
    public static $region_field2 = 'business_area_id';
    public static $region_field3 = 'city_code';

    public function __invoke(string $house_id, int $new_auction_status) {
        try{
            $house = (new House())->alias('h')
                ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
                ->field('ha.auction_status,h.village_id,h.business_area_id,h.city_code,h.is_shelf')
                ->find()
                ->toArray();
            $houseVillage = new HouseVillage();
            $businessArea = new BusinessArea();
            $region = new Region();
            $house_village = $houseVillage->where('id', $house[self::$region_field1])->findOrEmpty()->toArray();
            $business_area = $businessArea->where('id', $house[self::$region_field2])->findOrEmpty()->toArray();
            $_region = $region->where('city_code', $house[self::$region_field3])->findOrEmpty()->toArray();
            //前提是上架房源
            if ($house['is_shelf'] == 1) {
                //更新之前是待开始&进行中 并且更新之后不是待开始&进行中
                // 进行中的数量-1
                if (in_array($house['auction_status'], [1, 2]) && !in_array($new_auction_status, [1, 2])) {
                    $query1 = self::wh1($house_village, 'sale_num', $houseVillage, $house[self::$region_field1], 'id');
                    if ($query1) $query1->setDec('sale_num');

                    $query2 = self::wh1($business_area, 'house_number', $businessArea, $house[self::$region_field2], 'id');
                    if ($query2) $query2->setDec('house_number');

                    $query3 = self::wh1($_region, 'house_number', $region, $house[self::$region_field3], self::$region_field3);
                    if ($query3) $query3->setDec('house_number');
                }//更新之前是不待开始&进行中 并且更新之后是待开始&进行中
                // 进行中和总数+1
                elseif (!in_array($house['auction_status'], [1, 2]) && in_array($new_auction_status, [1, 2])) {
                    self::wh2($houseVillage, $house[self::$region_field1], 'id')->setInc('sale_num');
                    self::wh2($houseVillage, $house[self::$region_field1], 'id')->setInc('total_num');

                    self::wh2($businessArea, $house[self::$region_field2], 'id')->setInc('house_number');
                    self::wh2($businessArea, $house[self::$region_field2], 'id')->setInc('total_number');

                    self::wh2($region, $house[self::$region_field3], self::$region_field3)->setInc('house_number');
                    self::wh2($region, $house[self::$region_field3], self::$region_field3)->setInc('total_number');
                }
            }
        }catch (Exception $e){
            $err = "异常信息:{$e->getMessage()} | 异常所在的代码行:{$e->getLine()} | 异常文件{$e->getFile()} | php 版本 " . PHP_VERSION . "(" . PHP_OS . ")";
            Log::write($err, "HouseCount2_err.log");
        }
    }


}